package io.wongxd.demo.page.views

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.rememberDismissState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.wongxd.demo.M
import io.wongxd.solution.baseView.BaseComposeToolBarFgt
import io.wongxd.solution.compose.custom.views.chain_scrollable_component.swipe_to_dismiss.WSwipeToDismiss
import io.wongxd.solution.compose.custom.views.other.VerticalSpace
import io.wongxd.solution.compose.custom.views.touch_bar.StarBar
import io.wongxd.solution.compose.custom.views.util.rel
import io.wongxd.solution.compose.custom.views.util.rememberMutableStateOf

class FgtViewSwipeToDismiss : BaseComposeToolBarFgt() {
    override fun lazyInit() {

    }

    override val title: String
        get() = "FgtViewSwipeToDismiss"

    @Composable
    override fun Render() {
        ComposeContent()
    }

    private val width = 160.dp
    private val height = 80.dp

    @Composable
    private fun ComposeContent() {
        Column(modifier = Modifier.padding(start = 20.dp)) {
            Text("system:")
            SystemSwipeToDismiss()
            VerticalSpace(dp = 20)
            Text("composeViews:")
            ComposeViewsSwipeToDismiss(true)
            VerticalSpace(dp = 5)
            ComposeViewsSwipeToDismiss(false)
            VerticalSpace(dp = 5)
            Star()
        }
    }

    @Composable
    private fun Star() {
        var star by rememberMutableStateOf(value = 0)
        StarBar(
            starValue = star,
            onStarValueChange = { star = it },
            onTouchUpEvent = rel { println("star=$star") },
        )
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    private fun ComposeViewsSwipeToDismiss(contentIsMove: Boolean) {
        WSwipeToDismiss(
            minScrollPosition = 0.dp,
            maxScrollPosition = width / 2,
            backgroundContent = {
                Text(text = "删除",
                    Modifier
                        .fillMaxWidth()
                        .height(height)
                        .background(Color.Red)
                        .clickable { "删除".showToast() })
            }, contentIsMove = contentIsMove, modifier = M.size(width, height)
        ) {
            Text(
                text = "content",
                modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight()
                    .background(Color.LightGray)
            )
        }
    }

    @Composable
    @OptIn(ExperimentalMaterialApi::class)
    private fun SystemSwipeToDismiss() {
        val state = rememberDismissState()
        androidx.compose.material.SwipeToDismiss(state = state, background = {
            Text(
                text = "background", modifier = Modifier
                    .fillMaxSize()
                    .background(Color.Gray)
            )
        }, modifier = Modifier.size(width, height)) {
            Text(
                text = "content", modifier = Modifier
                    .fillMaxSize()
                    .background(Color.LightGray)
            )
        }
    }

    @Preview(showBackground = true, backgroundColor = 0xffffffff)
    @Composable
    fun RenderPreView() {

    }
}